package storage;

import general.Flag;
import general.Status;

import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Base and default implementation of StorageAdapter interface
 * 
 * @author Last G, ld
 */
public class Storage implements StorageAdapter
{
	// private Properties config;
	private StorageAdapter realStorage;
	private static final Logger logger = LoggerFactory.getLogger(Storage.class);

	/**
	 * creates a Storage object for flags
	 * 
	 * @param config
	 *            configurations
	 * @throws StorageException
	 *             can't create storage with this configs
	 * @author Last G
	 */
	public Storage(Properties config) throws StorageException
	{
		// this.config = config;
		if (config.getProperty("storage.type") == null)
		{
			logger.error("No such property 'storage.type' in configurations");
			throw new StorageException(
					"No such property 'storage.type' in configurations");
		}
		String dbType = config.getProperty("storage.type").toUpperCase();
		if (dbType.equalsIgnoreCase("MEMORY"))
		{
			logger.debug("Using Memory storage");
			realStorage = new Memory(config);
		} else if (dbType.equalsIgnoreCase("ORMLite"))
		{
			logger.debug("Using ORMLite storage");
			realStorage = new ORMLite(config);
		} else
		{
			logger.error("Incorrect configs: unknown DB Type: {}", dbType);
			throw new StorageException("Incorrect configs: unknown DB Type:"
					+ dbType);
		}
	}

	/**
	 * 
	 * @see storage.StorageAdapter#getFlags(int)
	 */
	@Override
	public Flag getFlag(int id) throws StorageException
	{
		return realStorage.getFlag(id);
	}

	/**
	 * 
	 * @see storage.StorageAdapter#getFlags(general.Status, int)
	 */
	@Override
	public Flag[] getFlags(Status state, int limit) throws StorageException
	{
		return realStorage.getFlags(state, limit);
	}

	/**
	 * @see storage.StorageAdapter#putFlag(general.Flag)
	 */
	@Override
	public int putFlag(Flag fl) throws StorageException
	{
		return realStorage.putFlag(fl);
	}

	/**
	 * @see storage.StorageAdapter#updateStatus(general.Flag[], general.Status)
	 */
	@Override
	public void updateStatus(Flag[] flags, Status[] statuses)
			throws StorageException
	{
		realStorage.updateStatus(flags, statuses);

	}

	@Override
	public Flag getFlagAndLock(int id) throws StorageException
	{
		return realStorage.getFlagAndLock(id);
	}

	@Override
	public Flag[] getFlagsAndLock(Status state, int limit)
			throws StorageException
	{
		return realStorage.getFlagsAndLock(state, limit);
	}

	@Override
	public void flushDB() throws StorageException
	{
		realStorage.flushDB();
		
	}

}
